<?php
/**
 *
 */
class Feed extends CI_Controller {

	function index() {
		/*
		 * show all feed
		 */
		include ("authenticate.php");
		
		$username = $_SESSION['username'];
		
		$this -> load -> model('feed_model');
		
		//pointer points to the first activity
		$pointer = 0;
		$numOfDataToGet = 5;
		
		$feeds = $this->feed_model->getFeeds($username, $numOfDataToGet, $pointer);	

		$data['feeds'] = $feeds;
		
		$this->load->view('view_feed', $data);
		
		
	}
	
	function getFeed(){
		/*
		 * AJAX
		 * get feeds based on the specified number of interval
		 */
		$pointer = $this->input->post('pointer');
		$limit = $this->input->post('interval');
		
		include ("authenticate.php");
		
		$username = $_SESSION['username'];
		
		$this -> load -> model('feed_model');
		
		$feeds = $this->feed_model->getFeeds($username, $limit, $pointer);	
		
		if(count($feeds)>0){
			//feed is present
			$header = false; // do not redirect
			
			$output = "";
			
			foreach ($feeds as $feed) {
				
				$subject;
				
				if(!isset($feed->user_username)){
					//activity from an idea
					$subject = "<a href = ".base_url("ideas/byId/$feed->idea_id")." >". $feed->idea_title."</a>";
				}else{
					//activity from a user
					$subject = "<a href = ".base_url("user/profile/$feed->user_username")." >".$feed->user_username. "</a>";
				};
								
				$output .= "<tr>\n";
				$output .= "<td class=\"bottomborder padding\">".ucfirst($subject . $feed->activity_context)."</td>";
				$output .= "<td class=\"bottomborder padding\" style=\"text-align: left; font-size: 10px\"> on ". $feed->activity_date ."</td>";
				$output .= "</tr>\n";
				
			}
		} else {
			$output = "No More Feed to Show";
		}
		
		echo json_encode($output);
		
	}
	
	function manage($type = NULL){
		/*
		 * manage all subscriptions
		 */
		
		if($type === NULL){
			//if null, automatically redirect to manage idea subscription	
			redirect(base_url("feed/manage/idea"));
			
		}else{
			
			include("authenticate.php");
			
			$username = $_SESSION['username'];
			$this->load->model('feed_model');
			
			if($type == "idea"){
				//get all subscribed ideas
				$subscriptions = $this->feed_model->getIdeaSubscriptionOfUser($username);
				
			}else if($type == "user"){
				//get all subscribed users
				$subscriptions = $this->feed_model->getUSerSubscriptionOfUser($username);
			}
			
			$data['subscriptions'] = $subscriptions;
			
			$this->load->view('view_manage_subscriptions', $data);
		}
	}


	function subscribeIdea() {
		/*
		 * AJAX
		 * handle idea subscription request and give appropriate response
		 */
		$this -> load -> model('feed_model');

		$idea_id = $this -> input -> post('idea_id');
		$username = $this -> input -> post('username');
		
		$successSubscribe = $this -> feed_model -> addSubscriptionIdea($idea_id, $username);

		if ($successSubscribe == 0) {
			echo "You are already subscribed to this idea";
		} else {
			//track activity
			$this -> feed_model -> trackUserActivity($username, " is now following an <a href = \"".base_url("ideas/byId/$idea_id")."\">idea.</a>");
			echo "Subscribed!";
		}
	}

	function unsubscribeIdea() {
		/*
		 * AJAX
		 * handle idea unsubscribe request
		 */	
		
		$this -> load -> model('feed_model');

		$idea_id = $this -> input -> post('idea_id');
		$username = $this -> input -> post('username');
		$successDelete = $this -> feed_model -> deleteSubscriptionIdea($idea_id, $username);

		if ($successDelete == 0) {
			echo "You are not subscribed to this idea";
		} else {
			echo "Unsubscribed!";
		}
	}

	function subscribeUser() {
		/*
		 * AJAX
		 * Handle user subscription request
		 */
		$this -> load -> model('feed_model');

		$username = $this -> input -> post('username');
		$followed_by_username = $this -> input -> post('followed_by_username');
		$successSubscribe = $this -> feed_model -> addSubscriptionUser($username, $followed_by_username);

		if ($successSubscribe == 0) {
			echo "You are already subscribed to this user";
		} else {
			echo "Subscribed!";
		}
	}

	function unsubscribeUser() {
		/*
		 * AJAX
		 * Handle user unsubscribe request
		 */
		$this -> load -> model('feed_model');

		$username = $this -> input -> post('username');
		$followed_by_username = $this -> input -> post('followed_by_username');
		$successDelete = $this -> feed_model -> deleteSubscriptionUser($username, $followed_by_username);

		if ($successDelete == 0) {
			echo "You are not subscribed to this user";
		} else {
			echo "Unsubscribed!";
		}
	}

}
?>